/**
 * ArithmaticGameView
 * 
 * @author Joshua Stein
 *
 */
package edu.bu.cs673.superkids123.children.view.games;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashMap;
import javax.swing.ImageIcon;
import javax.swing.JComponent;
import javax.swing.JPanel;

import edu.bu.cs673.superkids123.AbstractFrame;
import edu.bu.cs673.superkids123.AbstractNavigationView;
import edu.bu.cs673.superkids123.children.controller.games.ArithmaticGameController;
import edu.bu.cs673.superkids123.children.model.resources.AudioPlayer;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResource;
import edu.bu.cs673.superkids123.children.model.resources.ChildrenResourceProvider;
import edu.bu.cs673.superkids123.children.view.components.GraphicsImage;
import edu.bu.cs673.superkids123.children.view.components.ViewManager;
import edu.bu.cs673.superkids123.common.model.Child;
import edu.bu.cs673.superkids123.common.model.Child.Sex;
public class ArithmaticGameView extends AbstractNavigationView {
	
	public static final int NUMBER_OF_ANSWER_CHOICES = 5;
	private boolean readyToPaint = false;
	private JPanel arithmaticGamePanel;
	private ArithmaticGameController controller;
	private ChildrenResourceProvider provider = ChildrenResourceProvider.getInstance();
	private ImageIcon numberA, numberB, plusSign, next, option, background; //equalsSign,
	private ViewManager position;
	private HashMap<String, GraphicsImage> design;
	private boolean tryagain;//, firstPaint = true;
	
	public ArithmaticGameView(AbstractFrame mainFrame) {
		super(mainFrame);
		controller = getMainFrame().getController(ArithmaticGameController.class);
		setDesignAndImages();
		position = new ViewManager(design);
	}
	@Override
	public String getTitle() {
		return "Arithmatic Game";
	}
	@Override
	public String getBorderTitle() {
		return "Arithmatic Game";
	}
	@Override
	protected JComponent layout() {
		
		this.setParentsButtonVisible(false);
		this.setAvatarVisible(true);
		
		arithmaticGamePanel = new JPanel() {
			private static final long serialVersionUID = 1L;
			public void paint(Graphics g) {
				super.paint(g);
				if (readyToPaint) {	
					position.draw("background", g, background.getImage());
					if (tryagain) {
						g.drawString("Try again", 40, 200);
						
					}else{
						numberA = controller.currentQuestion.getImageA();
						numberB = controller.currentQuestion.getImageB();
					}
					for( int i=1; i<=3; i++){
						option = controller.currentQuestion.atAnswerOption(i);
						position.draw(String.valueOf(i), g, option.getImage());
					}
					position.draw("A", g, numberA.getImage());
					position.draw("plus", g, plusSign.getImage());
					position.draw("B", g, numberB.getImage());
					position.draw("->", g, next.getImage());					
				}
			}
		};
		HandlerClass handler = new HandlerClass(this);
		arithmaticGamePanel.addMouseListener(handler);
		readyToPaint = true;
		return arithmaticGamePanel;
	}
	public void tryAgainOff() {
		tryagain=false;
		reset();		
	}
	public void tryAgainOn() {
		tryagain=true;
		reset();
	}
	private void reset(){
		arithmaticGamePanel.revalidate();
		arithmaticGamePanel.repaint();
	}
	private class HandlerClass implements MouseListener{
		ArithmaticGameView view;
		public HandlerClass(ArithmaticGameView view){
			this.view = view;
		}
// ALL CLICK REACTIONS -- AUDIO AND CONTROLLER ACTIONS
		public void mouseClicked(MouseEvent p) {
			String clicked = position.whatsAt( p.getX(),  p.getY() );
			if(  clicked.equals(  "->"  ) ){
				controller.nextQuestion();
				tryAgainOff();
				view.arithmaticGamePanel.revalidate();
				view.arithmaticGamePanel.repaint();
			}
			if( clicked.equals(  "1"  )  |  clicked.equals(  "2"  ) | clicked.equals(  "3"  ) )	{	
				int optionNumber = Integer.parseInt(clicked);
				playAudioForNumber(controller.currentQuestion.getOption( optionNumber ) );	
				controller.answerButtonPressed( view, clicked );
			}
			if(clicked.equals("A")) playAudioForNumber(controller.currentQuestion.getA());
			if(clicked.equals("B")) playAudioForNumber(controller.currentQuestion.getB());
			if(clicked.equals("plus")) playAudio(ChildrenResourceProvider.getInstance().getAudioContent(ChildrenResource.audioPlus));
			
		}
// END
		public void mouseEntered(MouseEvent arg0) {}public void mouseExited(MouseEvent arg0) {}
		public void mousePressed(MouseEvent arg0) {}public void mouseReleased(MouseEvent arg0) {}
	}
	public void playAudio(byte[] audioData){
		AudioPlayer audioPlayer = new AudioPlayer();
		audioPlayer.playAudio(audioData);
		try {
			Thread.sleep(100);
		}
		catch (InterruptedException ie) {
			System.out.println("Thread interrupted");
		}
	}
	public void playAudioForNumber(int number) {
		AudioPlayer audioPlayer = new AudioPlayer();
		byte[] audioData = getAudioSampledData(number); 
		audioPlayer.playAudio(audioData);
		try {
			Thread.sleep(100);
		}
		catch (InterruptedException ie) {
			System.out.println("Thread interrupted");
		}
	}
	
	public byte[] getAudioSampledData(int numberToPlay) {
		byte[] audioData = null;
		audioData = provider.getAudioContentByNumber(numberToPlay);
		return audioData;
	}
	public void setBackground(){
		Sex sex =  ((Child) authController.getCurrentUser()).getSex(); 
		if (sex.equals(Child.Sex.Female))
			background = provider.getImage(ChildrenResource.arithmaticGameFemaleBackground);
		else if (sex.equals(Child.Sex.Male))
			background = provider.getImage(ChildrenResource.arithmaticGameMaleBackground);
		else
			background = provider.getImage(ChildrenResource.arithmaticGameMaleBackground);
	}
	private void setDesignAndImages(){
//LOAD Images
		//System.out.println(( (Child)authController.getCurrentUser()).getSex().toString()) ;
		//background = provider.getImage(ChildrenResource.arithmaticGameFemaleBackground);
		next = provider.getImage(ChildrenResource.next);//provider is used here, I don't like that it exists in different code areas at random.
		plusSign = provider.getImage(ChildrenResource.plusSign);
		//equalsSign = provider.getImage(ChildrenResource.equalsSign);
		design = new HashMap<String, GraphicsImage>();
// ALL DESIGN HERE
					/*put("name", new GraphicsImage("Name", pos_x, pos_y, width, height));*/
					design.put("background", new GraphicsImage("background", 0,0,400,800));
					design.put("A", new GraphicsImage("A", 0, 30, 150, 130));	
					design.put("plus", new GraphicsImage("plus", 145, 70, 50, 50));
					design.put("B", new GraphicsImage("B", 205, 30, 150, 150));
					//design.put("=", new GraphicsImage("=", 457, 50, 50, 50));
					design.put("1", new GraphicsImage("1", 560, 10, 80, 90));
					design.put("2", new GraphicsImage("2", 560, 100, 80, 90));
					design.put("3", new GraphicsImage("3", 560, 200, 80, 90));
					design.put("->", new GraphicsImage("->", 650, 0, 50, 50));
// END				
	}
}